package com.dkts.oil.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;

import java.util.*;

public class GeoOutUtil {
    public static org.locationtech.jts.geom.GeometryFactory locationtechGeometryFactory = new org.locationtech.jts.geom.GeometryFactory();

    public static String dataDrawLineString(Geometry geometryAll) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "FeatureCollection");

        int row=geometryAll.getNumGeometries();
        for(int i=0;i<row;i++){
            Geometry geometry=geometryAll.getGeometryN(i);
            Coordinate[] coordinates=geometry.getCoordinates();
            jsonArray.add(setGeoJSON(Arrays.asList(coordinates), "LineString"));
        }
        jsonObject.put("features", jsonArray);
        return jsonObject.toJSONString();
    }



    public static String dataDrawPolygon(Geometry geometryAll) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "FeatureCollection");

        int row=geometryAll.getNumGeometries();
        for(int i=0;i<row;i++){
            Geometry geometry=geometryAll.getGeometryN(i);
            Coordinate[] coordinates=geometry.getCoordinates();
            jsonArray.add(setGeoJSONPolygon(Arrays.asList(coordinates), "Polygon",geometry));
        }
        jsonObject.put("features", jsonArray);
        return jsonObject.toJSONString();
    }

    public static String dataDrawPolygon2(Geometry geometryAll) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "FeatureCollection");

        int row=geometryAll.getNumGeometries();
        for(int i=0;i<row;i++){
            Geometry geometry=geometryAll.getGeometryN(i);
            Coordinate[] coordinates=geometry.getCoordinates();
            jsonArray.add(setGeoJSONPolygon(Arrays.asList(coordinates), "Polygon",geometry));
        }
        jsonObject.put("features", jsonArray);
        return jsonObject.toJSONString();
    }

    public static JSONObject setGeoJSONPolygon( List<Coordinate> list, String geoType,Geometry geometry) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        geo.put("properties", new JSONObject());
        if (geometry != null) {
            geo.put("properties", geometry.getUserData());
        }
        JSONObject objectInfo = new JSONObject();
        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
        int coordRow = coordinates.length;
        List<double[]> gpsList = new ArrayList<>(coordRow);

        for (int h = 0; h < coordRow; h++) {
            Coordinate coordinate = coordinates[h];
            double[] gps = new double[2];
            gps[0] = coordinate.x;
            gps[1] = coordinate.y;
            gpsList.add(gps);
        }
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(gpsList);
        objectInfo.put("coordinates", jsonArray);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        return geo;
    }

    public static JSONObject setGeoJSON( List<Coordinate> list, String geoType) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        geo.put("properties", new JSONObject());
        JSONObject objectInfo = new JSONObject();
        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
        int coordRow = coordinates.length;
        List<double[]> gpsList = new ArrayList<>(coordRow);

        for (int h = 0; h < coordRow; h++) {
            Coordinate coordinate = coordinates[h];
            double[] gps = new double[2];
            gps[0] = coordinate.x;
            gps[1] = coordinate.y;
            gpsList.add(gps);
        }
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        return geo;
    }



    public static String getGeoJsonResult(List<Map<String,Object>> data) {
        List<Coordinate> dataList=new ArrayList<>();
        for(Map<String,Object> map:data){
           HashMap geometry=((HashMap)map.get("geometry"));
           if(geometry != null) {
               dataList= hashToCoordinates(geometry);
           }
        }
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "GeometryCollection");
        jsonArray.add(setGeoJSON(dataList, "LineString"));
        jsonObject.put("features", jsonArray);
        String json=jsonObject.toJSONString();
        System.out.println(" var polylineData2 =   "+json);
        return json;
    }

    public static List<Coordinate> hashToCoordinates(HashMap geometry) {

        String types=geometry.get("type") + "";
        List<Coordinate> dataList=new ArrayList<>();
        if ("Point".equals(types)) {
            ArrayList list = (ArrayList) geometry.get("coordinates");
            dataList.add(new Coordinate(Double.parseDouble(list.get(0) + ""), Double.parseDouble(list.get(1) + "")));
        } else if ("LineString".equals(types)) {
            ArrayList list = (ArrayList) geometry.get("coordinates");
            for (int i = 0; i < list.size(); i++) {
                ArrayList list2 = (ArrayList) list.get(i);
                dataList.add(new Coordinate(Double.parseDouble(list2.get(0) + ""), Double.parseDouble(list2.get(1) + "")));
            }
        } else if ("MultiPolygon".equals(types)) {
            ArrayList list = (ArrayList) geometry.get("coordinates");
            for (int i = 0; i < list.size(); i++) {
                ArrayList list2 = (ArrayList) list.get(i);
                for (int j = 0; j < list2.size(); j++) {
                    ArrayList list3 = (ArrayList) list2.get(j);
                    for (int h = 0; h < list3.size(); h++) {
                        ArrayList list4 = (ArrayList) list3.get(h);
                        dataList.add(new Coordinate(Double.parseDouble(list4.get(0) + ""), Double.parseDouble(list4.get(1) + "")));
                    }
                }

            }
        } else if ("Polygon".equals(types)) {
            ArrayList list = (ArrayList) geometry.get("coordinates");
            for (int i = 0; i < list.size(); i++) {
                ArrayList list2 = (ArrayList) list.get(i);
                for (int j = 0; j < list2.size(); j++) {
                    ArrayList list3 = (ArrayList) list2.get(j);
                    dataList.add(new Coordinate(Double.parseDouble(list3.get(0) + ""), Double.parseDouble(list3.get(1) + "")));
                }
            }
        } else if ("MultiLineString".equals(types)) {
            ArrayList list = (ArrayList) geometry.get("coordinates");
            for (int i = 0; i < list.size(); i++) {
                ArrayList list2 = (ArrayList) list.get(i);
                for (int j = 0; j < list2.size(); j++) {
                    ArrayList list3 = (ArrayList) list2.get(j);
                    dataList.add(new Coordinate(Double.parseDouble(list3.get(0) + ""), Double.parseDouble(list3.get(1) + "")));
                }
            }
        } else if ("MultiPoint".equals(types)) {
            ArrayList list = (ArrayList) geometry.get("coordinates");
            for (int i = 0; i < list.size(); i++) {
                ArrayList list2 = (ArrayList) list.get(i);
                dataList.add(new Coordinate(Double.parseDouble(list2.get(0) + ""), Double.parseDouble(list2.get(1) + "")));
            }
        }
        return dataList;
    }
    public static String setGeoJSONByCoordinate( List<Coordinate> list) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "GeometryCollection");
        jsonArray.add(setGeoJSON(list, "LineString"));
        jsonObject.put("features", jsonArray);
        return jsonObject.toJSONString();
    }

    public static String dataDrawPoint(List<Coordinate> list) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "GeometryCollection");
        for(int i=0;i<list.size();i++) {
            jsonArray.add(setGeoJSONCoordinate(list.get(i), "Point"));
        }
        jsonObject.put("features", jsonArray);
        return jsonObject.toJSONString();
    }

    public static JSONObject setGeoJSONCoordinate( Coordinate coordinate, String geoType) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject objectInfo = new JSONObject();
        double[] gpsList = new double[2];
        gpsList[0]=coordinate.x;
        gpsList[1]=coordinate.y;
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        return geo;
    }

    public static Geometry dataDrawGeometryON(JSONObject jsonObjectData) {
        JSONArray jsonArrayAll=jsonObjectData.getJSONArray("coordinates");

        Coordinate[] coordinates=new Coordinate[jsonArrayAll.size()];
        for (int i = 0; i < jsonArrayAll.size(); i++) {
            JSONArray jsonArray1 = jsonArrayAll.getJSONArray(i);
                Coordinate coordinate = new Coordinate(jsonArray1.getDouble(0), jsonArray1.getDouble(1));
                coordinates[i] = coordinate;
        }
        return locationtechGeometryFactory.createPolygon(coordinates);
    }

    public static Geometry dataDrawGeometryALL(JSONObject jsonObjectData) {
        JSONArray jsonArrayAll=jsonObjectData.getJSONArray("coordinates").getJSONArray(0);
        Coordinate[] coordinates=new Coordinate[jsonArrayAll.size()];
        for (int i = 0; i < jsonArrayAll.size(); i++) {
            JSONArray jsonArray1 = jsonArrayAll.getJSONArray(i);
                Coordinate coordinate = new Coordinate(jsonArray1.getDouble(0), jsonArray1.getDouble(1));
                coordinates[i] = coordinate;
        }
        return locationtechGeometryFactory.createPolygon(coordinates);
    }

    public static Geometry dataDraw4(String geometryJson,Geometry geometryA) {
        Geometry geometry = dataDrawGeometryON(JSONObject.parseObject(geometryJson));
        return geometryA.intersection(geometry);
    }


}
